$(function () {
    //获取文章id
    let id = new URLSearchParams(location.search).get('id')
    //评论列表
    function loadCommentList() {
        $.ajax({
            type: 'GET',
            url: 'http://localhost:8888/api/articles/' + id + '/comments',
            success: function (res) {
                console.log(res);
                if (res.status === 0) {
                    var arr = ['<h4><i class="sprites"></i>评论区</h4>']
                    var str = ""
                    res.data.forEach(function (item) {
                        let arr = item.cdate.split("T")
                        var time = arr.join("  ")
                        time = time.substring(0, time.length - 5)
                        str += `
                  <div class="kr_comment_card">
                    <div class="img-wrap">
                      <img src="./uploads/avatar_3.jpg" alt="">
                    </div>
                    <div class="info">
                      <p>${item.uname} · <span>${time}</span></p>
                      <p>${item.content}</p>
                    </div>
                    <a href="javascript:;" class="like">${item.count}</a>
                  </div>
                `
                    })
                    $('#comments_section').html(str)
                }
            }
        })
    }
    loadCommentList()
    // 发表评论
    $('#add').click(function () {
        let uname = $("#inputt").val()
        let content = $("#texta").val()
        $.ajax({
            type: 'POST',
            url: 'http://localhost:8888/api/articles/' + id + '/comments',
            data: {
                uname,
                content
            },
            success: function (res) {
                if (res.status === 0) {
                    // 发表评论成功
                    layer.msg(res.message)
                    //$('#comments_section').get(0).reset()
                    loadCommentList()
                }
            }
        })
    })
})
